package cn.hx.whh.automaticticket.pool;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

import cn.hx.whh.automaticticket.executor.PrintRunnable;

/**
 * 打印线程池执行器 (目前是单例线程)
 */
public class PrintThreadPoolExecutor {
    private static PrintThreadPoolExecutor poolExecutor;
    private static int maxPoolSize = 1;//最大线程数
    private static int minPoolSize = 1;//最小线程数
    private static long hangDuration = 5000L;// 多久内没有任务时挂起 (5秒)
    private static long complete = 0L; // 任务完成时间节点毫秒数
    private static List<WordThread> listThread;
    private static BlockingQueue<PrintRunnable> workQueue = new LinkedBlockingDeque<>(100);

    /**
     * 注册并获取线程池
     *
     * @return
     */
    public static PrintThreadPoolExecutor getInstance() {
        if (poolExecutor == null) {
            listThread = new ArrayList<>(minPoolSize);
            for (int i = 0; i < minPoolSize; i++) {
                WordThread thread = new WordThread() {
                    @Override
                    public void run() {
                        while (true) {
                            PrintRunnable printRunnable;
                            synchronized (workQueue) {
                                printRunnable = workQueue.poll();
                            }
                            if (printRunnable != null) {
                                printRunnable.run();
                            } else {
                                if (complete != 0L && complete != 1L) {
                                    complete = System.currentTimeMillis();
                                } else {
                                    if ((System.currentTimeMillis() - complete) > hangDuration) {
                                        try {
                                            this.wait();
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        complete = 1L;
                                    }
                                }
                            }

                        }
                    }
                };
                listThread.add(thread);
                thread.start();
            }

            try {
                Thread.sleep(5100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return poolExecutor == null ? new PrintThreadPoolExecutor() : poolExecutor;
    }

    private PrintThreadPoolExecutor() {
    }

    /**
     * 提交任务线程
     *
     * @param print
     * @return
     */
    public synchronized static boolean excute(PrintRunnable print) {
        if (poolExecutor == null) {
            getInstance();
        }
        boolean boo = workQueue.offer(print);
        if (complete == 1L) {
            for (WordThread thread : listThread) {
                thread.notify();
            }
        }
        return boo;
    }
}

abstract class WordThread extends Thread {

}